package demo.config;

import com.jfinal.config.*;
import com.jfinal.json.JFinalJson;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.jfinal.server.undertow.UndertowServer;
import demo.interceptor.DemoInterceptor;
import demo.model._MappingKit;
//import demo.model.User;

public class DemoConfig extends JFinalConfig {


    public void configConstant(Constants me) {
        me.setDevMode(true);
        // 开启对 jfinal web 项目组件 Controller、Interceptor、Validator 的注入
        me.setInjectDependency(true);

        // 开启对超类的注入。不开启时可以在超类中通过 Aop.get(...) 进行注入
        me.setInjectSuperClass(true);

        //驼峰命名法配置
        JFinalJson.setModelAndRecordFieldNameToCamelCase();
    }

    public void configRoute(Routes me) {
        // jfinal 4.9.03 版新增了路由扫描功能，不必手动添加路由
        // me.add("/hello", HelloController.class);

        // 使用路由扫描，参数 "demo." 表示只扫描 demo 包及其子包下的路由
        me.scan("demo.");
    }

    public void configEngine(Engine me) {
        me.setDevMode(true);
        me.setBaseTemplatePath("/view/");
        me.setToClassPathSourceFactory();
    }
    public void configPlugin(Plugins me) {
        DruidPlugin dp = new DruidPlugin("jdbc:mysql://121.41.229.122/jfinal-test?" +
                "useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&userSSL=true&serveTimezone=GMT%2B8",
                "root", "123456");
        me.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        me.add(arp);
       // arp.addMapping("test", User.class);
        arp.setShowSql(true);
        //arp.addMapping("article", "article_id", Article.class);
        arp.addSqlTemplate("all.sql");
        _MappingKit.mapping(arp);
    }

    public void configInterceptor(Interceptors me) {
        // 添加控制层全局拦截器
        me.addGlobalActionInterceptor(new DemoInterceptor());
    }
    public void configHandler(Handlers me) {}
}
